/*
 * Copyright (C) 2012 attmore
 * e-mail: attmore@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.pocketsweet.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.AvatarView;
import com.pocketsweet.ui.uilib.GalleryView;
import com.pocketsweet.utils.ToolKits;

/**
 * @author attmore
 */
public class GalleryAdapter extends BaseAdapter {
	private Context mContext;
	private List<String> mUploadFilePaths;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	@SuppressWarnings("deprecation")
	public GalleryAdapter(Context x, List<String> uploadfiles) {
		mContext = x;
		mUploadFilePaths = uploadfiles;
		options = new DisplayImageOptions.Builder().cacheInMemory()
				.cacheOnDisc().build();
		imageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
//		if (mUploadFilePaths == null) {
//			return 1;
//		} else if (mUploadFilePaths.size() < GalleryView.MAX_IMAGE_NUM) {
//			return mUploadFilePaths.size() + 1;
//		} else {
//			return mUploadFilePaths.size();
//		}
		return 8;
	}

	@Override
	public Object getItem(int position) {
		if (mUploadFilePaths != null) {
			return mUploadFilePaths.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (mUploadFilePaths == null) {
			AvatarView imageView = new AvatarView(mContext);
			imageView.setImageNull();
			return imageView;
		} else if (mUploadFilePaths.size() < GalleryView.MAX_IMAGE_NUM) {
			
			if (position < mUploadFilePaths.size()) {
				AvatarView imageView = new AvatarView(mContext);
				// imageloader
				String filePath;
				if (mUploadFilePaths.get(position).indexOf("http") == 0) {// 判断是否含有http开头
					filePath = mUploadFilePaths.get(position);
				} else {
					filePath = "file://" + mUploadFilePaths.get(position);
				}
				if (filePath
						.equals(UserService.getCurrentUser().getAvatarUrl())) {
					imageView.setAvatarText();
				}
				imageView.setPhoto(filePath);
				return imageView;
			} else {
				AvatarView imageView = new AvatarView(mContext);
				if(position==mUploadFilePaths.size()){					
					imageView.setImageResource();
				}else{
					imageView.setImageNull();
				}
				return imageView;
			}
		} else {
			AvatarView imageView = new AvatarView(mContext);
			String filePath;
			if (mUploadFilePaths.get(position).indexOf("http") == 0) {// 判断是否含有http开头
				filePath = mUploadFilePaths.get(position);
			} else {
				filePath = "file://" + mUploadFilePaths.get(position);
			}
			if (filePath.equals(UserService.getCurrentUser().getAvatarUrl())) {
				imageView.setAvatarText();
			}
			imageView.setPhoto(filePath);
			return imageView;
		}
	}
}
